The goal of this document is to show an example of using leaflet R package.
I will use real dataset to show data on the map. This allows me to demonstrate some real case of using the package.
I chose to use city of Chicago open dataset which can be downloaded from City of Chicago Data Portal (https://data.cityofchicago.org/).
I use Crimes dataset (https://data.cityofchicago.org/Public-Safety/Crimes-2001-to-present/ijzp-q8t2) which contains all registered climes in Chicago. I’ve downloaded filtered data. I only selected data for 2015 and 2016 year. I’ve also filtered out all the data without Latitude and Longitude.
data <- read.csv("Crimes_-_2001_to_present.csv")
summary(data)
## ID Case.Number Date
## Min. : 21714 HZ140230: 6 01/01/2015 12:01:00 AM: 97
## 1st Qu.:10141055 HZ403466: 4 01/01/2015 12:00:00 AM: 88
## Median :10360298 HZ554936: 4 01/01/2015 09:00:00 AM: 54
## Mean :10340226 HY259141: 3 09/01/2015 09:00:00 AM: 49
## 3rd Qu.:10587784 HY442430: 3 05/01/2015 09:00:00 AM: 47
## Max. :10816322 HZ260932: 3 06/01/2015 09:00:00 AM: 47
## (Other) :511025 (Other) :510666
## Block IUCR Primary.Type
## 001XX N STATE ST : 1565 0486 : 48399 THEFT :115139
## 0000X W TERMINAL ST : 950 0820 : 47508 BATTERY : 97266
## 008XX N MICHIGAN AVE: 857 0460 : 31080 CRIMINAL DAMAGE: 58715
## 076XX S CICERO AVE : 702 1320 : 28489 ASSAULT : 35026
## 0000X N STATE ST : 636 1310 : 26941 NARCOTICS : 33503
## 083XX S STEWART AVE : 469 0810 : 26796 OTHER OFFENSE : 33314
## (Other) :505869 (Other):301835 (Other) :138085
## Description Location.Description
## SIMPLE : 54527 STREET :119794
## DOMESTIC BATTERY SIMPLE: 48399 RESIDENCE : 81004
## $500 AND UNDER : 47508 APARTMENT : 66009
## TO VEHICLE : 30042 SIDEWALK : 49965
## TO PROPERTY : 26941 OTHER : 20317
## OVER $500 : 26796 PARKING LOT/GARAGE(NON.RESID.): 15203
## (Other) :276835 (Other) :158756
## Arrest Domestic Beat District
## false:394622 false:428637 Min. : 111 Min. : 1.00
## true :116426 true : 82411 1st Qu.: 613 1st Qu.: 6.00
## Median :1024 Median :10.00
## Mean :1146 Mean :11.23
## 3rd Qu.:1711 3rd Qu.:17.00
## Max. :2535 Max. :31.00
##
## Ward Community.Area FBI.Code X.Coordinate
## Min. : 1.00 Min. : 0.00 06 :115139 Min. :1094231
## 1st Qu.:10.00 1st Qu.:23.00 08B : 82404 1st Qu.:1152564
## Median :23.00 Median :32.00 14 : 58715 Median :1166096
## Mean :22.91 Mean :37.23 26 : 48644 Mean :1164459
## 3rd Qu.:34.00 3rd Qu.:56.00 18 : 32912 3rd Qu.:1176362
## Max. :50.00 Max. :77.00 05 : 26696 Max. :1205117
## NA's :2 (Other):146538
## Y.Coordinate Year Updated.On
## Min. :1813897 Min. :2015 08/17/2015 03:03:40 PM:150895
## 1st Qu.:1858932 1st Qu.:2015 04/15/2016 03:49:27 PM: 7853
## Median :1892278 Median :2015 09/10/2015 11:43:14 AM: 6458
## Mean :1885938 Mean :2015 10/09/2015 03:58:54 PM: 6016
## 3rd Qu.:1908615 3rd Qu.:2016 08/31/2015 03:43:09 PM: 5056
## Max. :1951535 Max. :2016 09/17/2015 11:37:18 AM: 4789
## (Other) :329981
## Latitude Longitude Location
## Min. :41.64 Min. :-87.93 (41.883500187, -87.627876698): 980
## 1st Qu.:41.77 1st Qu.:-87.72 (41.754592961, -87.741528537): 699
## Median :41.86 Median :-87.67 (41.897895128, -87.624096605): 539
## Mean :41.84 Mean :-87.67 (41.742710224, -87.634088181): 437
## 3rd Qu.:41.90 3rd Qu.:-87.63 (41.976290414, -87.905227221): 399
## Max. :42.02 Max. :-87.52 (41.979006297, -87.906463155): 395
## (Other) :507599
As a simple example let’s show the first 500 crimes from the dataset.
library(leaflet)
library(dplyr)
head(data, 500) %>% select(Latitude, Longitude) %>% leaflet() %>% addTiles() %>% addMarkers(clusterOptions = markerClusterOptions())
Now let’s create heatmaps of crimes in 2015-2016 in summary and also by the most presented types of crimes.
For heatmap we need to install leaflet.extras package (details here - https://github.com/bhaskarvk/leaflet.extras).
All crimes in 2015-2016
library(leaflet.extras)
data %>% select(Latitude, Longitude) %>% leaflet() %>% addTiles() %>% addWebGLHeatmap(lng=~Longitude, lat=~Latitude, size=500)